import 'package:flutter/material.dart';
import 'package:hm_show_flutter/api/service/category_service.dart';
import 'package:hm_show_flutter/entity/category_parent_entity.dart';
import 'package:hm_show_flutter/event/category_event.dart';
import 'package:hm_show_flutter/event/company_event.dart';
import 'package:hm_show_flutter/page/category/category_child_view.dart';
import 'package:hm_show_flutter/page/category/category_parent_view.dart';
import 'package:hm_show_flutter/page/category/category_parent_view.dart'
    as prefix0;
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/toast_util.dart';
import 'package:hm_show_flutter/widgets/empty_view.dart';

///分类
class CategoryView extends StatefulWidget {
  @override
  _CategoryViewState createState() => _CategoryViewState();
}

class _CategoryViewState extends State<CategoryView> {
  CategoryService _categoryService = new CategoryService();
  List<CategoryParentDataList> _listParent;
  List<CategoryParentDataListCategory> _listChild;

  void _getCategoryData() {
    _categoryService.getCategoryParentData((success) {
      CategoryParentEntity entity = success;
      setState(() {
        _listParent = entity.data.xList;
        _listChild = _listParent[0].category;
      });
    });
  }

  Widget categoryView() {
    return Container(
        child: Row(children: <Widget>[
      Expanded(
        flex: 2,
        child: CategoryParentView(_listParent, (data) {
          setState(() {
            _listChild = data;
          });
        }),
      ),
      Expanded(
        flex: 8,
        child: CategoryChildView(_listChild),
      ),
    ]));
  }

  _registerEventBus() {
    companyEventBus.on<CompanyEvent>().listen((CompanyEvent loginEvent) {
      _getCategoryData();
    });
    categoryEventBus.on<CategoryEvent>().listen((CategoryEvent event) {
      _getCategoryData();
    });

  }

  @override
  void initState() {
    super.initState();
    _getCategoryData();
  }

  @override
  Widget build(BuildContext context) {
    _registerEventBus();
    return Scaffold(
      appBar: PreferredSize(
          child: AppBar(
            backgroundColor: AppColors.mainColor,
          ),
          preferredSize: Size.zero),
      body: _listParent != null
          ? categoryView()
          : EmptyView(
              onRetry: () {
                _getCategoryData();
              },
            ),
    );
  }
}
